﻿#include <QtDebug>
#include <QThread>
#include <QFile>
#include <QTextStream>
#include <QMetaType>
#include "file_process.h"
#include "../data_modules/track_data.h"
#include "../data_modules/constant_variable.h"

File_process::File_process(QObject *parent)
    : QObject(parent)
{
    qRegisterMetaType<Track_data*>("Track_data*");
}

File_process::~File_process()
{

}

void File_process::sl_open_files_to_show(QStringList files)
{
    QString file_name("");

    foreach (file_name, files) {
        QFile file(file_name);

        if ( !file.exists()) {
            continue;
        }

        if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
            QTextStream in(&file);
            in.setAutoDetectUnicode(true);

            QString content("");
            int row(0);

            while (0 != (content = in.readLine()).length()) {
                emit si_send_track_to_model(get_track_data(row, content));
                ++row;
            }


            emit si_quit_sub_thread(1);
        }
    }
}

void File_process::sl_split_data_to_files(QStringList files_name)
{
    QString file_name_wiht_path;

    foreach (file_name_wiht_path, files_name) {
        QFile file(file_name_wiht_path);

        if ( !file.exists()) {
            continue;
        }

        if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
            QTextStream in(&file);

            // 自动监测Unicode
            in.setAutoDetectUnicode(true);

            // Create file to save data
            QFile out_process_file(file_name_wiht_path.replace(EXTENSION_LOG,
                                                               EXTENSION_PROCESS_LOG,
                                                               Qt::CaseInsensitive));
            QFile out_message_file(file_name_wiht_path.replace(EXTENSION_PROCESS_LOG,
                                                               EXTENSION_MESSAGE_LOG,
                                                               Qt::CaseInsensitive));

            // Open two files with write only and truncate pattern
            if (out_process_file.open(QIODevice::WriteOnly | QIODevice::Truncate)
                    && (out_message_file.open(QIODevice::WriteOnly | QIODevice::Truncate))) {
                QTextStream out_process(&out_process_file);
                QTextStream out_message(&out_message_file);

                QString content;
                // Read data by lines and save data to files
                while (0 != (content = in.readLine()).length()) {
                    if (content.contains("sysRecv 1 received message")) {
                        out_message<< content.replace("sysRecv 1 received message", "") << Qt::endl;
                    } else if (content.contains("sysRecv 1 process")) {
                        out_process<< content.replace("sysRecv 1 process ", "") << Qt::endl;
                    }
                }
            }

            emit si_quit_sub_thread(1);
        }
    }
}

Track_data *File_process::get_track_data(int row, QString content)
{
    Track_data* data(new Track_data());
    data->setup_data(content);

    // 显示航迹数据，设置数据在文件的序号row
    data->set_show_track(true);
    data->set_row(row);

    return data;
}
